<template>
  <NConfigProvider
    v-if="!isLock"
    :locale="zhCN"
    :theme="getDarkTheme"
    :theme-overrides="getThemeOverrides"
    :date-locale="dateZhCN"
  >
    <AppProvider>
      <RouterView />

      <!-- 添加更新检查器组件 -->
      <PortableUpdateChecker />
    </AppProvider>
  </NConfigProvider>

  <transition v-if="isLock && $route.name !== 'login'" name="slide-up">
    <LockScreen />
  </transition>
</template>

<script lang="ts" setup>
  import { computed, onMounted, onUnmounted } from 'vue';
  import { zhCN, dateZhCN, darkTheme } from 'naive-ui';
  import { LockScreen } from '@/components/Lockscreen';
  import { AppProvider } from '@/components/Application';
  import { useScreenLockStore } from '@/store/modules/screenLock.js';
  import { useRoute } from 'vue-router';
  import { useDesignSettingStore } from '@/store/modules/designSetting';
  import { lighten } from '@/utils/index';
  import PortableUpdateChecker from '@/components/Update/PortableUpdateChecker.vue';

  const route = useRoute();
  const useScreenLock = useScreenLockStore();
  const designStore = useDesignSettingStore();
  const isLock = computed(() => useScreenLock.isLocked);
  const lockTime = computed(() => useScreenLock.lockTime);
  const autoLockEnabled = computed(() => useScreenLock.autoLockEnabled);

  /**
   * @type import('naive-ui').GlobalThemeOverrides
   */
  const getThemeOverrides = computed(() => {
    const appTheme = designStore.appTheme;
    const lightenStr = lighten(designStore.appTheme, 6);
    return {
      common: {
        primaryColor: appTheme,
        primaryColorHover: lightenStr,
        primaryColorPressed: lightenStr,
        primaryColorSuppl: appTheme,
      },
      LoadingBar: {
        colorLoading: appTheme,
      },
    };
  });

  const getDarkTheme = computed(() => (designStore.darkTheme ? darkTheme : undefined));

  let timer: NodeJS.Timer;

  const timekeeping = () => {
    // 如果自动锁屏功能被禁用，则不执行自动锁屏逻辑
    if (!autoLockEnabled.value) return;

    clearInterval(timer);
    if (route.name == 'login' || isLock.value) return;

    // 重置锁屏时间
    useScreenLock.setLockTime();

    timer = setInterval(() => {
      // 锁屏倒计时递减
      useScreenLock.setLockTime(lockTime.value - 1);
      if (lockTime.value <= 0) {
        // 设置锁屏
        useScreenLock.setLock(true);
        return clearInterval(timer);
      }
    }, 1000);
  };

  // 初始化锁屏计时器
  const initLockTimer = () => {
    if (!autoLockEnabled.value) return;
    if (route.name == 'login' || isLock.value) return;

    // 初始化锁屏时间
    useScreenLock.setLockTime();

    timer = setInterval(() => {
      useScreenLock.setLockTime(lockTime.value - 1);
      if (lockTime.value <= 0) {
        useScreenLock.setLock(true);
        return clearInterval(timer);
      }
    }, 1000);
  };

  onMounted(() => {
    // 只有在自动锁屏功能启用时才添加事件监听器和初始化计时器
    if (autoLockEnabled.value) {
      document.addEventListener('mousedown', timekeeping);
      document.addEventListener('keydown', timekeeping);
      // 页面加载时启动锁屏计时器
      initLockTimer();
    }
  });

  onUnmounted(() => {
    document.removeEventListener('mousedown', timekeeping);
    document.removeEventListener('keydown', timekeeping);
    clearInterval(timer);
  });
</script>
